/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.motoo.sample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.code.motoo.core.IoHandlerAdapter;
import com.google.code.motoo.core.IoSession;

public class EchoIoHandler extends IoHandlerAdapter {
	private static StringBuilder sb = new StringBuilder();
	private static int length = 0;
	static {
//		sb.append("<html><head><title>online game</title></head>");
//		sb.append("<body>");
		sb.append("<b>");
		length = sb.length();
	}
	
	public final static Log log = LogFactory.getLog(EchoIoHandler.class);
	
	private AtomicInteger count = new AtomicInteger(0);
	private Random random = new Random();
	private List<IoSession> containor = Collections.synchronizedList(new ArrayList<IoSession>());

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
//		cause.printStackTrace();
		sessionClosed(session);
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		log.info("message---" + message);
		sb.setLength(length);
		sb.append("1111111111111111");
		sb.append("</BR>");
		sb.append(message.toString());
		sb.append("</b>");
		session.write(sb.toString());
		log.info("message write......");
		
		Thread.sleep(3000);
		sb.setLength(length);
		sb.append("22222222222");
		sb.append("</BR>");
		sb.append(message.toString());
		sb.append("</b>");
		session.write(sb.toString());
		log.info("message write......");
		
		Thread.sleep(3000);
		sb.setLength(length);
		sb.append("33333333333333");
		sb.append("</BR>");
		sb.append(message.toString());
		sb.append("</b>");
		session.write(sb.toString());
		log.info("message write......");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		for (IoSession value : containor) {
			if (value.equals(session)) {
				containor.remove(value);
				count.decrementAndGet();
				break;
			}
		}
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		String address = session.getRemoteAddress().toString();
		if (address.startsWith("/192.168.1.205")) return;
		int index = count.getAndIncrement();
		System.out.println("session count : " + index);
		containor.add(session);
	}
	
	
	

}
